package com.example;

public class ThreadWaitingTest4 {

    public static class TestThread extends Thread{

        private final Object shareObject;

        public TestThread(Object shareObject){
            super("test");
            this.shareObject = shareObject;
        }

        @Override
        public void run() {

            synchronized (shareObject){
                try {
                    System.out.println("test线程即将进入waiting状态...");
                    shareObject.wait();
                    System.out.println("测试线程继续执行");
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }

        }
    }

    public static void main(String[] args) throws InterruptedException {
        Object shareObject = new Object();
        TestThread thread = new TestThread(shareObject);
        thread.start();

        Thread.sleep(1000*5);
        synchronized (shareObject){
            System.out.println("主线程抢到了shareObject的监视器锁");
            System.out.println("此时test线程的状态为："  + thread.getState());
            System.out.println("主线程开始调用shareObject.notify()方法...");
            shareObject.notify();
            System.out.println("shareObject.notify()方法已经调用，此时test线程的状态为：" + thread.getState());
            System.out.println("主线程开始睡上30秒，以确保在此期间主线程一直占有shareObject的监视器锁");
            Thread.sleep(1000 * 30);
            System.out.println("主线程即将释放shareObject的监视器锁");
        }
    }
}
